package demointerface;
import java.util.Arrays;
import java.util.Comparator;

class Student implements Comparable<Student> {
    public String name;
    public int age;
    public Student(String name,int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public  String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ",age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //return this.name.compareTo(name);//比较姓名
        return this.age  - o.age;//比较年龄
    }
}
//比较器
class AgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}
class NameComparator implements Comparator<Student> {
    public int compare(Student o1,Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
public class Test {
    //自我实现冒泡
    public static void mySort(Comparable[] comparable) {
        for(int i = 0;i < comparable.length - 1;i++)  {
            for(int j = 0; j < comparable.length - i - 1;j++) {
                if(comparable[j].compareTo(comparable[j + 1]) > 0)
                {
                    Comparable tmp = comparable[j];
                    comparable[j] = comparable[j + 1];
                    comparable[j + 1] = tmp;
                }
            }
        }
    }
    public static void main(String[] args) {
        Student student1 = new Student("zhangsan",20);
        Student student2 = new Student("lisi",40);
        //比较器
        AgeComparator ageComparator = new AgeComparator();
        System.out.println(ageComparator.compare(student1,student2));
        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(student1,student2));

        System.out.println("=================");
        Student[] student = new Student[3];
        student[0] = new Student("zhangsan",20);
        student[1] = new Student("lisi",40);
        student[2] = new  Student("wangwu",30);

        //Arrays.sort(student,ageComparator);//传年龄比较器,使用年龄进行比较
        Arrays.sort(student,nameComparator);//传姓名,使用姓名比较

        System.out.println(Arrays.toString(student));
    }
    public static void main2(String[] args) {
        Student[] student = new Student[3];
        student[0] = new Student("zhangsan",20);
        student[1] = new Student("lisi",40);
        student[2] = new  Student("wangwu",30);

        //Arrays.sort(student);
        mySort(student);
        System.out.println(Arrays.toString(student));
    }
    public static void main1(String[] args) {
        Student student1 = new Student("zhangsan",19);
        Student student2 = new Student("lisi",20);
        if(student1.compareTo(student2) > 0)  {
            System.out.println("student1 > student2");
        }else if(student1.compareTo(student2) < 0) {
            System.out.println("student1 < student2");
        }else {
            System.out.println("student1 == student2");
        }
//        if(student1.compareTo(student2) > 0) {
//            System.out.println("student1 > student2");
//        }else {
//            System.out.println("student1 < student2");
//        }
    }
}
